clear all
set more off

global path "~/shared_space/dbaharc/BHOR-YugoslavianRefugees/"
qui do "${path}do_files/InitializeGlobals.do"

/********************/
/*** Prepare Data ***/
/********************/


/*Appendix: redoing at the W93 3-digit  level*/
use "${path}data/mig_fyug_est_v300.dta", clear
ren nation nation2
merge m:1 nation2 year using "${path}data/ah100d01_mig_pop_fyug.dta", keep(master matched) nogen keepusing(mig_asyl)
//Create shares by dividing by total asylum seekers per year
ds mig_est*, v(32)
local vlist = r(varlist)
foreach v of local vlist {
    local vname = subinstr("`v'", "mig", "share",1)
    g `vname' = `v'/mig_asyl
}

ren nation2 iso3 
replace iso3 = "YUG" if iso3=="FYUG"
//keep if year==1975 | year==1980 | year==1990 | year==2000 | year==2010
keep if year==1991 | year==1992 | year==1993 | year==1994 | year==1995 
egen countryproduct = group(w93_3 iso3)
qui ds mig* share*, v(32)
local vlist = r(varlist)
foreach v of local vlist {
    local vv = subinstr("`v'","_","",1)
    ren `v' `vv'_
}
reshape wide mig* share*, i(countryproduct) j(year)
qui ds migest_asyl* shareest_asyl*
local ivlist = r(varlist)
forvalues y =1991/1995 {
    local ivlist = subinstr("`ivlist'", "_`y'", "",.)
}
local ivlist : list uniq ivlist
//di "`ivlist'"
foreach var of local ivlist {
	egen `var'_9195 = rowmean(`var'*) //Avg expected asylum seekers by industry 1991-1995
}
keep w93_3 *_9195
g nation2="FYUG"
merge 1:1 nation2 w93_3 using "${path}data/mig_fyug_rm4_v300.dta", keep(master using matched) nogen
drop if nation2!="FYUG"
drop nation2
g isic = floor(w93_3/10)
gcollapse (sum) mig* share*, by(isic) 
tempfile YUGmighatwide
save `YUGmighatwide'

use "${path}data/UNIDOexYUG.dta", clear
gcollapse (sum) establishments emp output valueadded capital female_emp (mean) wages index_industrial_prod, by(isic year)
g iso3 ="YUG"
drop if isic=="D"
destring isic, replace
merge m:1 isic using `YUGmighatwide', keep(master matched) nogen

g temp  = emp if year==1990
egen emp1990 = max(temp), by(isic)
drop temp

g temp  = emp if year==2000
egen emp2000 = max(temp), by(isic)
drop temp

//New treatment (checks that they actually left the sample, not only the tradable sector)
foreach y in 2000 2005 2010 {
    g treat`y' = asinh(mig_rm_left_sam_`y')
    g treat`y'pw = asinh(mig_rm_left_sam_`y'/(emp1990/1000))
    g after`y' = year>=`y'
    g didsample1990`y' = year==1990 | year==`y' 
    g didsample1995`y' = year==1995 | year==`y'
}
g treat1995level = asinh(mig_rm_came_sam_9195)
g treatIV = asinh(shareest_asyl_DEU_9195) //IV uses distrubition (shares) of Germans in 1990
ren isic product

/*Main specification */
preserve
replace emp = . if emp==0
replace capital = . if capital==0
g asinhprod = asinh(output) if !mi(emp)
g asinhprodpw = asinh(output/emp)
g asinhvadd = asinh(valueadded) if !mi(emp)
g asinhvaddpw = asinh(valueadded/emp)
g asinhwage = asinh(wage) if !mi(emp)
g asinhemp = asinh(emp)
g asinhfirms = asinh(establishments) if !mi(emp)
g asinhinvestment = asinh(capital) if !mi(emp)
g asinhinvestmentpw = asinh(capital/emp)

//Capital formation using the perpetual inventory
//Depreciation set to be 4%
//Series start in 1986, in US$
//Merge with US GDP deflator from WDI
g countrycode="USA" //We use the USA deflator, so artificially assign this variable to every observation
merge m:1 countrycode year using "${path}data/WDIv2015.dta", keep(master matched) keepusing(ny_gdp_defl_zs) nogen
drop countrycode
ren ny_gdp_defl_zs gdpdeflator2010

g real_investment = capital/(gdpdeflator2010/100)
g asinhrinvestment = asinh(real_investment)
g asinhrinvestmentpw = asinh(real_investment/emp)

sort product year
g Kstock = real_investment if year==1986
bysort product: replace Kstock = Kstock[_n-1]*0.96 + real_investment if year>1986

g asinhcapstock = asinh(Kstock)
g asinhcapstockpw = asinh(Kstock/emp)

label var asinhprodpw "$ asinh(prodpw) $"
label var asinhvaddpw "$ asinh(vaddpw) $"
label var asinhwage "$ asinh(wage) $"

foreach v in asinhprodpw asinhvaddpw asinhwage {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000#c.$after if year==1990 | year==2005, a(product year) cluster(product) old
}
estout, $estout_params_txt order(c.treat2000#c.after2005)
estout using "${path}tables/DIDUNIDO.tex", replace $estout_params $estout_pre $estout_post //mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear

foreach v in asinhprodpw asinhvaddpw asinhwage {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000pw#c.$after  if year==1990 | year==2005, a(product year) cluster(product) old
}
estout, $estout_params_txt order(c.treat2000pw#c.after2005)
estout using "${path}tables/APP-DIDUNIDOpw.tex", replace $estout_params $estout_pre $estout_post //mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear

foreach v in asinhprodpw asinhvaddpw asinhwage {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000#c.$after asinhinvestmentpw if year==1990 | year==2005, a(product year) cluster(product) old
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000#c.$after asinhcapstockpw if year==1990 | year==2005, a(product year) cluster(product) old
}
estout, $estout_params_txt order(c.treat2000#c.after2005)
estout using "${path}tables/DIDUNIDO_Capital.tex", replace $estout_params $estout_pre $estout_post //mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear

// SINGLE TABLE FOR BOTH UNIDO AND BOSNIA DATA
foreach v in asinhprodpw asinhvaddpw asinhwage {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000#c.$after if year==1990 | year==2005, a(product year) cluster(product) old
}
estout, $estout_params_txt order(c.treat2000#c.after2005)
estout using "${path}tables/DIDUNIDOandBosnia.tex", replace $estout_params $estout_pre_panelA 
eststo clear
restore


clear all
global path "~/shared_space/dbaharc/BHOR-YugoslavianRefugees/"
qui do "${path}do_files/InitializeGlobals.do"

/********************/
/*** Prepare Data ***/
/********************/
use "${path}data/BosniaDatasetW93.dta", clear
ren treatYug treat2000
ren treatBosnian treat9699
ren treatBosnian2 treatBIH2000
ren treatCroatian treatHRV2000

g temp = no_persons if year==1990
egen emp1990 = max(temp), by(w93_2)
drop temp

g treat9699pw = asinh(mig_trt_end9699/(emp1990/1000))
g treatBIH2000pw = asinh(mig_trt_bih/(emp1990/1000))

g asinhworkers = asinh(no_persons)
g asinhsales = asinh(total_production)
g asinhsalespw = asinh(total_production/no_persons)
g asinhfirms = asinh(no_entreprises)
g asinhexppw = asinh(export/no_persons)
g asinhwpf = asinh(no_persons/no_entreprises)

label var asinhexppw "$ asinh(exppw) $"
label var asinhfirms "$ asinh(firms) $"
label var asinhworkers "$ asinh(workers) $"

//lnexp lnexpplus1 asinhexp are exports for Bosnia only

g after2010 = year>=2010
ren exp_share1990 germanexpshare1990

label var germanexpshare1990 ""
label var treat2000 ""
label var treat9699 ""
label var treatBIH2000 ""

/*
foreach v in asinhsales asinhfirms asinhworkers asinhsalespw {
    replace `v'=0 if mi(`v') & asinhexp==0
}
*/

g insample = !mi(asinhfirms) & !mi(asinhworkers)

foreach v in asinhexppw asinhfirms asinhworkers {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treatBIH2000#c.after2010  if insample, a(w93_2 year) cluster(w93_2) old
}
estout using "${path}tables/DIDBosnianData.tex", replace $estout_params $estout_pre $estout_post order(c.treat*)
estout, $estout_params_txt order(c.treat*)
eststo clear


foreach v in asinhexppw asinhfirms asinhworkers {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treatBIH2000pw#c.after2010  if insample, a(w93_2 year) cluster(w93_2) old
}
estout using "${path}tables/APP-DIDBosnianDatapw.tex", replace $estout_params $estout_pre $estout_post order(c.treat*)
estout, $estout_params_txt order(c.treat*)
eststo clear

//Table for both UNIDO and Bosnia (appending)
foreach v in asinhexppw asinhfirms asinhworkers {
    local lbl`v' : variable label `v'    
    eststo, title(`lbl`v''): qui reghdfe `v' c.treatBIH2000#c.after2010  if insample, a(w93_2 year) cluster(w93_2) old
}
estout using "${path}tables/DIDUNIDOandBosnia.tex", append $estout_params $estout_pre_panelB $estout_post order(c.treat*)
estout, $estout_params_txt order(c.treat*)
eststo clear

/*
foreach v in asinhexp asinhexppw  {
     eststo, title(`v'): qui reghdfe `v' c.treatBIH2000#c.after2010  if insample, a(w93_2 year) cluster(w93_2) old
     eststo, title(`v'): qui reghdfe `v' c.treatBIH2000pw#c.after2010  if insample, a(w93_2 year) cluster(w93_2) old
}
estout, $estout_params_txt order(c.treat*)
eststo clear



/*
foreach v in asinhexp asinhfirms asinhworkers asinhexppw {
    eststo, title(`v'): qui reghdfe `v'  (c.treat9699#c.after2010=c.treatIV#c.after2010) if insample, a(w93_2 year) cluster(w93_2) old
    eststo, title(`v'): qui reghdfe `v'  (c.treatBIH2000#c.after2010=c.treatIV#c.after2010) if insample, a(w93_2 year) cluster(w93_2) old
}
//estout using "${path}tables/DIDBosnianData.tex", replace $estout_params $estout_pre $estout_post
estout, $estout_params_IV_txt order(c.treat*)
eststo clear
*/
